<?php
define('IN_ADAGPS', TRUE);
define('ROOTURL', 'index.php');
$sTplRootUrl = ROOTURL;
global $arrConfig,$generalTitle,$lang,$tbl_prefix,$modulecall,$ajax,$strMainMenu,$objXajax,$objDbSelect;
$tbl_prefix = TBL_PREFIX;
include_once('cfg.php');
include_once(CORE_PATH.'includes/constant_common.php');// chứa các hằng số sử dụng chung cho cả Vi và EN
include_once(CORE_PATH.'includes/functions.php');
include_once(CORE_PATH.'includes/functions_xajax.php');
include_once(CORE_PATH.'database/cls_table.php');
include_once(CORE_PATH.'database/cls_table_search.php');
include_once(CORE_PATH.'database/commonDB.php');
$objXajax->registerFunction('ajxLogout');
$objXajax->registerFunction('ajxLoadContent');
$objXajax->registerFunction('frmindex_save');
$objXajax->registerFunction('frmchangepass_save');

$tbl_prefix = TBL_PREFIX;
$sys_active = ACTIVE;
$sys_deactive = DEACTIVE;

if(!ConnectDB())
{
    if( $_REQUEST['xajax']==null){
                 echo $objDbSelect->ErrorMsg(), '<br>';
    }       
    if($arrConfig['debug'] === true)
    {
        Die(CONNECT_DB_ERR);
    }else{
        $_POST['xajax']='ajxLogout';
    }
    Die(CONNECT_DB_ERR);
}

if(IsLogin()){
    $sModule = GetModule();
    if($sModule=='logout'){// trường hợp bấm nút logout
        logout();
        CloseDB();
    }else{
    $tplXajaxScript = ''; 
    $powertype = $_SESSION['current']['power_type_id'];
    include('module_management.php'); 
    
    if($_REQUEST['xajax']==null){      
    //Start tao main menu
     //   include('module_management.php'); 
//        $sqlMainMenu = " select name_$lang as name, id as id,icon as icon from tbl_main_menu where active =  $sys_active and id in
//    ( select main_menu_id from tbl_mainmenu_powertype where powertype_id = '$powertype')";
//        $arrMainMenu = $objDbSelect->GetArray($sqlMainMenu);
//        $strMainMenu = createMainMenu($arrMainMenu);
//        $sIndexFile = TEMPL_PATH.$lang.'/index.html';
//        $objTemplate->LoadTemplate($sIndexFile, 'UTF-8');   
//        $strnamedisplay = getNameToDisplay();
//        $menu = CreateListMenu();     
    $power_type_id = $_SESSION['current']['power_type_id'];
    $sql = "select * from tbl_main_menu WHERE id in (select main_menu_id from tbl_mainmenu_powertype where power_type_id ='$power_type_id') AND active = $sys_active";
    $arMenu = $objDbSelect->GetArray($sql);
    $last = 0;
    for($i = 0; $i < count($arMenu); $i++) 
    {
        if($i == 0)
            $arMenu[$i]['row'] = ' ui-corner-left';
        else
            $arMenu[$i]['row'] = '';
        $arMenu[$i]['index'] = $i;
        $last = $i;
    }
    if($last > 0)
        $arMenu[$last]['row'] = ' ui-corner-right';

    $sIndexFile = TEMPL_PATH.$lang.'/index.html';
    $objTemplate->LoadTemplate($sIndexFile, 'UTF-8');
   // echo '<pre>';print_r($arMenu);die();
    $objTemplate->MergeBlock('menu', $arMenu);
    $objTemplate->MergeBlock('menu2', $arMenu);
    
    }else{
        $fnName   = $_REQUEST['xajax'];
        $arrName  = explode('_',$fnName);
        $nameFile = str_replace('frm','main_',$arrName[0]) ;
       // include(MODULE_PATH.'management/'.$nameFile.'.php');
    }
    
    $objXajax->processRequests();
    $tplXajaxScript = $tplXajaxScript.$objXajax->getJavascript('scripts/xajax');
    $objTemplate->Show(); 
    CloseDB(); 
    }
    
    

}else{
    if($_REQUEST['xajax']!=null){
        $_POST['xajax']='ajxLogout';
        $objXajax->processRequests();
    }else{
        $tplXajaxScript = '';    
        $sModule = GetModule(true);        
        $tmplContent = TEMPL_PATH.$lang."/login/main_$sModule.html";
        $sIncFile = MODULE_PATH.'login/main_'.$sModule.'.php';
        $sIndexFile = TEMPL_PATH.$lang.'/home.html';
        if(!file_exists($tmplContent)){
          $tmplContent = TEMPL_PATH.$lang."/login/main_login.html";  
        }
        $objTemplate->LoadTemplate($sIndexFile, 'UTF-8');   
        $strnamedisplay = getNameToDisplay();
        if(file_exists($sIncFile))
        {
            include_once($sIncFile);
        }
        $objXajax->processRequests();
        $tplXajaxScript = $tplXajaxScript.$objXajax->getJavascript('scripts/xajax');
        CloseDB();
        // Show page
        $objTemplate->Show();   
    }
    
}

function ajxLogout($arrParams=null)
{
    $objResponse = new xajaxResponse();
    session_destroy();
    $objResponse->addRedirect('index.php?m=login');
    return $objResponse->getXML();
}
function logout()
{
    global $objDbSelect,$sessionID;
    session_destroy();    
    Redirect('index.php?m=login'); 
    exit;  
}//
function getModule($login=false){ 
global  $arrModuleLogin;  
    $module = 'login';    
    if(isset($_GET['m'])){
      $temp = $_GET['m'];
      if($login){          
          if(in_array($temp,$arrModuleLogin)){
            $module = $temp;  
          }
      }else{
        $module = $temp; 
      }
    }
    return $module;
    
}

function ajxLoadContent($main_menu_id)
{
    global $objDbSelect,$lang,$main_menu;
    $active= ACTIVE;
    $objResponse = new xajaxResponse();
    $power_type_id = $_SESSION['current']['power_type_id'];
    $lang = isset($_SESSION['lang'])?$_SESSION['lang']:'vi';
    $user_id = $_SESSION['current']['user_id'];
    $sql = "select distinct *,name_$lang as menuname from tbl_menu where (main_menu_id = $main_menu_id
    and id in  (select menu_id from tbl_menu_powertype where power_id= '$power_type_id' and active =$active)
    and active = $active and menu_type = 0) or id in (select menu_id from tbl_menu_advance ma join tbl_menu m on m.id =
    ma.menu_id and m.main_menu_id = '$main_menu_id' and m.menu_type =1 where ma.user_id = '$user_id' and ma.active =$active and m.active = $active )";   
    $arr = $objDbSelect->GetArray($sql);   
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('templates/'.$lang.'/page_main.html');
    $oTbs->MergeBlock('blk_data', $arr);
    $oTbs->Show(TBS_NOTHING);
    $main_tabs = "main_tabs_$main_menu_id";
    $objResponse->addAssign($main_tabs, 'innerHTML', $oTbs->Source);
    $objResponse->addScript("createTab('$main_tabs');");
   // $objResponse->addScript();
        
    return $objResponse->getXML();
}

function frmindex_save($arrparams)
{
    global $objDbSelect;
     $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $name = addslashes($arrparams['txtName']);
    $mail = $arrparams['txtMail'];
    $phone = $arrparams['txtPhone'];
    $title = addslashes($arrparams['txtSubj']);
    $content =addslashes($arrparams['txtMesg']) ;
    $sql = " insert into tbl_feedback(name,mail,phone,title,content,date_created,user_created)
     value('$name','$mail','$phone','$title','$content',now(),'$user_id')";   
     $objDbSelect->Execute($sql);
    $objResponse->addScript('changeWaiting(1);'); 
    $objResponse->addScript("$('#frmindex_feedback').dialog('close');"); 
    return $objResponse->getXML();
}
function frmchangepass_save($arrparams)
{
    global $objDbSelect;
     $objResponse = new xajaxResponse();
    $user_id = $_SESSION['current']['user_id'];
    $oldpass = EncryptPass($arrparams['txtOldPass']);
    $newpass = $arrparams['txtNewPass'];
    $newpassconfirm = $arrparams['txtNewPassConfirm'];
    if($newpass!=$newpassconfirm){
      $objResponse->addScript("$('#frmchangepass_divalert').text('Mật khẩu mới và mật khẩu xác nhận không trùng');");  
    }else{
        $sqlconfirm = "select count(id) as rowcount from tbl_user where pass = '$oldpass' and id = '$user_id'";
        $arr = $objDbSelect->GetArray($sqlconfirm);
        if($arr[0]['rowcount']>0&&is_array($arr)){
            $newpass = EncryptPass($newpass);
           $sqlupdate = "update  tbl_user SET pass = '$newpass' WHERE id = '$user_id'";
           $objDbSelect->Execute($sqlupdate);
           $objResponse->addScript("$('#frmchangepass_dialog').dialog('close');");   
        }else{
           $objResponse->addScript("$('#frmchangepass_divalert').text('Mật khẩu cũ sai, vui lòng nhập lại!');");
        }     
    
    }
  $objResponse->addScript('changeWaiting(1);');
  return $objResponse->getXML();
}




